﻿using Halcon二次开发.Controller.CamerForm交互;
using Halcon二次开发.Controller.数据加载与保存;
using Halcon二次开发.Model;
using Halcon二次开发.Model.储放所有单例.储存每个单例界面;
using Halcon二次开发.Model.图像绑定流程图显示;
using Halcon二次开发.Model.自定义控件;
using Halcon二次开发.View.工位监控画面;
using halcon联合.自定义控件;
using MvCameraControl;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;

namespace Halcon二次开发.View.相机
{
    public partial class CamerForm : Form
    {
        /// <summary>
        ///实时图像窗体 
        /// </summary>
        RealTimeCamerForm realTimeCamerForm;
        public CamerFormController controller;
        /// <summary>
        /// 当前工位的界面
        /// </summary>
        public HwindowFlowNodeUrseControl HwindowFlowNode;
        public CamerForm(HwindowFlowNodeUrseControl hwindowFlowNode)
        {
            InitializeComponent();
            this.HwindowFlowNode = hwindowFlowNode;
            tabControl1.SelectedIndex = 1;
            //实例化一次动作类
            realTimeCamerForm = new RealTimeCamerForm();//生成一个实时现实图像的窗口
            controller = new CamerFormController(hwindowFlowNode, realTimeCamerForm,this);
            this.MinimizeBox = false;
        }
        public void SetJsonPaths(string path, string addres)
        {
            string paths = path + "\\" + "界面数据\\" + addres + "\\" + addres + "数据.json";
            //调用存储方法
            new LoadData().LoadDatas(paths, this);

            //自动连接相机功能
            string oldSer = comboBox1.Text;
            button5_Click(this, null);
            for (int i = 0; i < comboBox1.Items.Count; i++)
            {
                if (comboBox1.Items[i].ToString() == oldSer)
                {
                    comboBox1.SelectedIndex = i;
                    button6_Click(this, null);
                    //确认配置
                    try
                    {
                        button9_Click(this, null);
                    }
                    catch { }
                }
            }
        }
        /// <summary>
        /// 打开文件夹
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            controller.OpenDirectory(textBox1, flowLayoutPanel1,label1);
        }

        private void CamerForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }
        /// <summary>
        /// 打开文件
        /// </summary>
        /// <param name="sender"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            controller.OpenFile(textBox1,flowLayoutPanel1,label1);
        }
        /// <summary>
        /// 刷新按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
       
        private void button5_Click(object sender, EventArgs e)
        {
            List<IDeviceInfo>  deviceInfos= controller.RefreshDeviceList();
            if (deviceInfos != null)
            {
                comboBox1.Items.Clear();
                foreach (IDeviceInfo item in deviceInfos)
                {
                    comboBox1.Items.Add(item.DevTypeInfo.ToString() + ":" + item.ModelName + ":" + item.SerialNumber);
                }
            }
        }
        /// <summary>
        /// 初始化取相
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex != -1)
            {
                if (controller.OpenCamer(comboBox1.SelectedIndex))
                {
                    label8.Text = "已初始化取相";
                    label8.ForeColor = System.Drawing.Color.Red;
                    button6.Enabled = false;
                    button7.Enabled = true;
                    //读取当前设备信息
                    controller.GetCamerFoloat();
                }
            }
            HwindowFlowNode.camerFormController = controller;//将工位相机对象给到自定义控件
        }
        /// <summary>
        /// 关闭取相按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
            private void button7_Click(object sender, EventArgs e)
        {
            if (controller.CloseCamer())
            {
                label8.Text = "未初始化取相";
                label8.ForeColor = System.Drawing.Color.Black;
                button6.Enabled = true; ;
                button7.Enabled = false;
                comboBox2.Items.Clear();
                comboBox3.Items.Clear();
                comboBox4.Items.Clear();
                comboBox5.Items.Clear();
                comboBox6.Items.Clear();
                comboBox1.SelectedIndex = -1;
                comboBox1.Text = "";
                controller.triggerSourcePaper = false;
                controller.triggerModePaper = false;
                HwindowFlowNode.framesTimeForm.indicatorLight1.State = IndicatorLight.IndicatorState.GrayStable; // 切换为灰色稳定
            }
        }
        /// <summary>
        /// 运行按钮 获取当前一帧的图像
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>+
       private void toolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {
                controller.Run();
            }
            catch (Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name+":" +ex.Message+"\r\n");
            }
        }
        /// <summary>
        /// 打开实时图像按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (realTimeCamerForm != null)
            {
                realTimeCamerForm.Show();
            }
        }
        /// <summary>
        /// 设置增益效果
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {
            controller.SetGain(float.Parse(numericUpDown2.Value.ToString()));
        }
        private void CamerForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            controller.Closed();
        }
        /// <summary>
        /// 帧率改变时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numericUpDown4_ValueChanged(object sender, EventArgs e)
        {
            controller.SetAcquisitionFrameRate(float.Parse( numericUpDown4.Value.ToString()));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedIndex = 0;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedIndex = 1;
        }
        /// <summary>
        /// 修改相机配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button9_Click(object sender, EventArgs e)
        {
            try
            {
                controller.SetCamerMode();
                controller.SetOutMode();
                if (controller.hwindowFlowNode.runPaper == false)
                {
                    return;
                }
                if (controller.triggerSourcePaper && controller.triggerModePaper && HwindowFlowNode.runPaper)
                {
                    HwindowFlowNode.framesTimeForm.indicatorLight1.State = IndicatorLight.IndicatorState.GreenStable; // 切换为绿色稳定
                }
                else
                {
                    HwindowFlowNode.framesTimeForm.indicatorLight1.State = IndicatorLight.IndicatorState.GrayStable; // 切换为绿色稳定
                }
            }
            catch
            {

            }
           
        }
        //属性值更改时
        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private int delayInterval = 0; // 延迟时间（单位：毫秒）
        private DateTime lastClickTime = DateTime.MinValue; // 上一次点击的时间

        // 电平反转测试
        private void button10_Click(object sender, EventArgs e)
        {
            // 获取当前时间
            DateTime currentTime = DateTime.Now;

            // 判断当前点击与上一次点击的时间间隔是否大于延迟时间
            if ((currentTime - lastClickTime).TotalMilliseconds > delayInterval)
            {
                // 更新上一次点击时间
                lastClickTime = currentTime;

                // 动态更新延迟时间
                delayInterval = (int)numericUpDown5.Value;
                if (delayInterval < 0) delayInterval = 0; // 确保非负数

                // 执行电平反转信号逻辑
                controller.LevelReversal();
            }
        }
        //恢复默认设置
        private void button8_Click(object sender, EventArgs e)
        {
            try
            {
                comboBox2.SelectedIndex = 0;
                comboBox3.SelectedIndex = 0;
                comboBox4.SelectedIndex=0;
                comboBox5.SelectedIndex=0;
                comboBox6.SelectedIndex=0;
            }
            catch { }

        }

        private void CamerForm_Load(object sender, EventArgs e)
        {

        }
        //当属性值修改时更新输出信息
        private void comboBox7_SelectedIndexChanged(object sender, EventArgs e)
        {
            controller.SetOutMode();
        }

        /// <summary>
        /// 设置相机IP
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button11_Click_1(object sender, EventArgs e)
        {
            try
            {
                //设置IP,成功则往下
                controller.SetIP();
            }
            catch (Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(ex.Message + "\r\n");
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox2.Enabled = true;
            button11.Enabled = true;
        }

        /// <summary>
        /// 反射调用该方法更新窗体句柄
        /// </summary>
        /// <param name="hwindowFlowNode"></param>
        public void PushHWindon(HwindowFlowNodeUrseControl hwindowFlowNode)
        {
            this.HwindowFlowNode = hwindowFlowNode;
            controller.hwindowFlowNode = hwindowFlowNode;
            SetJsonPaths($"Data\\工程文件\\{hwindowFlowNode.projectName}\\{hwindowFlowNode.name}任务文件", StaicResouce.CopyTreeNode.Text);
        }
        /// <summary>
        /// 设置曝光时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numericUpDown1_ValueChanged_1(object sender, EventArgs e)
        {
            controller.SetExposure(float.Parse(numericUpDown1.Value.ToString()));
        }
    }
}
